package it.elis.media.library.dao.springImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;

import it.elis.media.library.dao.UserDao;
import it.elis.media.library.model.data.Role;
import it.elis.media.library.model.data.User;

public class UserDaoSpringImpl extends NamedParameterJdbcDaoSupport implements
		UserDao {

	Logger logger = Logger.getLogger(this.getClass());

	private final String searchUserBasicQuery = "select  u.prog_user_id,"
			+ " u.username," + " u.password," + " u.name," + " u.surname,"
			+ " r.prog_role_id," + " r.code_role," + " r.desc_role"
			+ " from role r, user u" + " where u.prog_role_id = r.prog_role_id";

	private final String getRolesForUserQuery = "select r.prog_role_id, r.code_role, r.desc_role "
			+ "from role r, user u "
			+ "where u.prog_role_id = r.prog_role_id "
			+ "and u.username = :username";

	@Override
	public List<User> search(User userCriteria) {
		StringBuilder builder = new StringBuilder();

		builder.append(searchUserBasicQuery);
		Map<String, String> parameterMap = new HashMap<String, String>();

		if (null != userCriteria.getUserName()) {
			parameterMap.put("userName", userCriteria.getUserName());
			builder.append(" and u.username = :userName");
		}

		logger.info(builder.toString());

		return getNamedParameterJdbcTemplate().query(builder.toString(),
				parameterMap, new UserMapper());
	}

	@Override
	public User getUser(String userName) {
		User userCriteria = new User();
		userCriteria.setUserName(userName);
		List<User> list = search(userCriteria);
		switch (list.size()) {
		case 0:
			return null;
		case 1:
			return list.get(0);
		default:
			// Impossibile: throw an Exception
			logger.error("Numero risultati " + list.size() + " IMPOSSIBILE!!!");
			return null;
		}
	}

	@Override
	public List<Role> getRoles(String username) {
		Map<String, String> parameterMap = new HashMap<String, String>();
		parameterMap.put("username", username);
		return getNamedParameterJdbcTemplate().query(getRolesForUserQuery,
				parameterMap, new RoleMapper());
	}

}

class UserMapper implements RowMapper<User> {

	@Override
	public User mapRow(ResultSet arg0, int arg1) throws SQLException {
		Role role = new Role();

		role.setProgRoleId(arg0.getInt("prog_role_id"));
		role.setCodeRole(arg0.getString("code_role"));
		role.setDescRole(arg0.getString("desc_role"));

		User user = new User();
		user.setRole(role);

		user.setProgUserId(arg0.getInt("prog_user_id"));
		user.setUserName(arg0.getString("username"));
		user.setPassword(arg0.getString("password"));
		user.setName(arg0.getString("name"));
		user.setSurname(arg0.getString("surname"));

		return user;
	}

}

class RoleMapper implements RowMapper<Role> {

	@Override
	public Role mapRow(ResultSet arg0, int arg1) throws SQLException {
		Role role = new Role();

		role.setProgRoleId(arg0.getInt("prog_role_id"));
		role.setCodeRole(arg0.getString("code_role"));
		role.setDescRole(arg0.getString("desc_role"));

		return role;
	}

}
